#include "stdafx.h"
#include "common.h"
#include "minmax_normalizer.h"

MinMaxNormalizer::MinMaxNormalizer(){
	name_ = "minmax";
	dimension_ = -1;
	num_ = -1;
}

void MinMaxNormalizer::normalization(shared_ptr<DataBlob> data){
	dimension_ = data->get_dimension();
	num_ = data->get_num();
	min_ = vector<double>(dimension_, INT16_MAX);
	max_ = vector<double>(dimension_, INT16_MIN);
	vector<vector<double>>& features = data->features_;
	// get min and max vector
	for (int i = 0; i < num_; i++) {
		for (int j = 0; j < dimension_; j++) {
			if (features[i][j] < min_[j])
				min_[j] = features[i][j];
			if (features[i][j] > max_[j])
				max_[j] = features[i][j];
		}
	}
	for (int i = 0; i < dimension_; i++)
		max_[i] = max_[i] - min_[i];
	// actual normalization process
	for (int i = 0; i < num_; i++) {
		for (int j = 0; j < dimension_; j++) {
			features[i][j] = (features[i][j] - min_[j]) / max_[j];
		}
	}
}

void MinMaxNormalizer::print_normalizer() const {
	if (num_ == -1)
		cout << "empty minmax normalizer" << endl;
	else {
		cout << "num: " << num_ << endl;
		cout << "dimension: " << dimension_ << endl;
		for (int i = 0; i < dimension_; i++)
			cout << min_[i] << " ";
		cout << endl;
		for (int i = 0; i < dimension_; i++)
			cout << max_[i] << " ";
		cout << endl;
	}
}